$(function(){
    $("#publishDemand").click(function(){
        window.location.href = server_url + '/buyer/demand/publishDemand.do';
    })
    findSource();
})
var sourceNum = 0;
function findSource(page)
{
    if(page){
        sourceNum = page;
    }
    mineNum = 0;
    supplyNum = 0;
    if(sourceNum == 0)
    {
        $("#list").html("");
    }else{
        $("#spilter").remove();
        $("#more").remove();
    }
    $.ajax({
        url:server_url + '/rest/buyer/demand/source.do',
        data:{pageNum:sourceNum,userId:userid,ran:Math.random()},
        success:function(data){
            console.log(data);
            if (sourceNum == 0 && (data.data ==null || data.data.length == 0)){
                var none = '<div class="none_box">\
                    <p>\
                    <span>抱歉，没有找到符合条件的商品。 </span>\
                <br />\
                <span>&nbsp;</span>\
                </p>\
                <input type="button" onclick="window.location.href=\'tel:4008637797\'" value="联系客服" />\
                </div>';
                $("#list").html(none);
                return;
            }
            mapIfExist(function(data){
                var html = '<div class="det_box" onclick="toDetail(\''+data.id+'\')">\
                            <img src="/LianniuWeb/img/cow/img.png" alt="" class=""/>\
                        <div class="">\
                        <div class="con_tit">\
                        <p>'+data.details[0].productname+'</p>\
                        </div>\
                        <p class="con_normal">类别：'+data.details[0].categoryName+'<span class="fr">浏览：'+data.clickcount +'</span></p>\
                        <p class="con_normal">采购数量：'+data.details[0].amount +'头<span class="fr">报价：'+data.supplycount +'</span></p>\
                        <p class="con_normal">期望价格：'+data.details[0].minprice +'-'+data.details[0].maxprice +'/头</p>\
                        <p class="con_normal">有效期：至'+data.deadline.substring(0,10) +'</p>\
                        </div>\
                        </div>';
                $("#list").append(html);
            },data.data);
            var more = '<div class="gray" id="spilter"></div><div id="more" onclick="javascript:findSource()" class="det_box" >\
            <p style="\
            text-align: center;\
            ">加载更多</p>    \
            </div>';
            $("#list").append(more);
            sourceNum++;
        }
    })
}
function toDetail(id)
{
    window.location.href = server_url + '/buyer/demand/demandDetail.do?id='+id;
}
var mineNum = 0;
function minePublish(page)
{
    if(page){
        mineNum = page;
    }
    sourceNum = 0
    supplyNum = 0;
    if(mineNum == 0)
    {
        $("#list").html("");
    }else{
        $("#spilter").remove();
        $("#more").remove();
    }
    $.ajax({
        url:server_url+'/rest/buyer/demand/mine.do',
        data:{userId:userid,pageNum:mineNum,ran:Math.random()},
        success:function(data)
        {
            console.log(data);
            if (mineNum == 0 && (data.data ==null || data.data.length == 0)){
                var none = '<div class="none_box">\
                    <p>\
                    <span>抱歉，没有找到符合条件的商品。 </span>\
                <br />\
                <span>请发布需求，我们将尽快与您联系！</span>\
                </p>\
                <input type="button" onclick="window.location.href=\'tel:4008637797\'" value="联系客服" />\
                </div>';
                $("#list").html(none);
                return;
            }
            mapIfExist(function(data){
                var html = '<div class="det_box">\
                            <img src="/LianniuWeb/img/cow/img.png" alt="" class=""/>\
                        <div class="">\
                        <div class="con_tit">\
                        <p>'+data.details[0].productname+'</p>\
                        </div>\
                        <p class="con_normal">类别：'+data.details[0].categoryName+'<span class="fr">浏览：'+data.clickcount +'</span></p>\
                        <p class="con_normal">采购数量：'+data.details[0].amount +'头<span class="fr">报价：'+data.supplycount +'</span></p>\
                        <p class="con_normal">期望价格：'+data.details[0].minprice +'-'+data.details[0].maxprice +'/头</p>\
                        <p class="con_normal">有效期：至'+data.deadline.substring(0,10) +'</p>\
                        </div>\
                        </div>';
                $("#list").append(html);
            },data.data);
            var more = '<div class="gray" id="spilter"></div><div id="more" onclick="javascript:minePublish()" class="det_box" >\
            <p style="\
            text-align: center;\
            ">加载更多</p>    \
            </div>';
            $("#list").append(more);
            mineNum++;
        }
    })
}
var supplyNum = 0;
function supply(page)
{
    if(page){
        supplyNum = page;
    }
    mineNum = 0;
    sourceNum = 0;
    if(supplyNum == 0)
    {
        $("#list").html("");
    }else{
        $("#spilter").remove();
        $("#more").remove();
    }
    $.ajax({
        url:server_url+'/rest/buyer/demand/supplys.do',
        data:{userId:userid,pageNum:mineNum,ran:Math.random()},
        success:function(data)
        {
            console.log(data);
            if (supplyNum == 0 && (data.data ==null || data.data.length == 0)){
                var none = '<div class="none_box">\
                    <p>\
                    <span>您还没有任何报价。 </span>\
                <br />\
                <span>可以到货源中报价！</span>\
                </p>\
                <input type="button" onclick="window.location.href=\'tel:4008637797\'" value="联系客服" />\
                </div>';
                $("#list").html(none);
                return;
            }
            mapIfExist(function(data){
                data = data.demandVo;
                var html = '<div class="det_box">\
                            <img src="/LianniuWeb/img/cow/img.png" alt="" class=""/>\
                        <div class="">\
                        <div class="con_tit">\
                        <p>'+data.details[0].productname+'</p>\
                        </div>\
                        <p class="con_normal">类别：'+data.details[0].categoryName+'<span class="fr">浏览：'+data.clickcount +'</span></p>\
                        <p class="con_normal">采购数量：'+data.details[0].amount +'头<span class="fr">报价：'+data.supplycount +'</span></p>\
                        <p class="con_normal">期望价格：'+data.details[0].minprice +'-'+data.details[0].maxprice +'/头</p>\
                        <p class="con_normal">有效期：至'+data.deadline.substring(0,10) +'</p>\
                        </div>\
                        </div>';
                $("#list").append(html);
            },data.data);
            var more = '<div class="gray" id="spilter"></div><div id="more" onclick="javascript:minePublish()" class="det_box" >\
            <p style="\
            text-align: center;\
            ">加载更多</p>    \
            </div>';
            $("#list").append(more);
            supplyNum++;
        }
    })
}